package com.bangda.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bangda.crm.domain.enums.OrderStatusEnum;
import com.bangda.crm.util.UplpadUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bangda.crm.domain.CrmOrder;
import com.bangda.crm.service.ICrmOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户订单Controller
 * 
 * @author fanboshi
 * @date 2023-10-17
 */
@RestController
@RequestMapping("/crm/order")
public class CrmOrderController extends BaseController
{
    @Autowired
    private ICrmOrderService crmOrderService;

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file)throws Exception {
        String path = UplpadUtil.ossUpload(file);
        return success(path);
    }
    @PreAuthorize("@ss.hasPermi('crm:order:edit')")
    @Log(title = "客户订单", businessType = BusinessType.UPDATE)
    @PutMapping("/shipments")
    public AjaxResult shipments(@RequestBody CrmOrder crmOrder)
    {
        crmOrder.setOrderStatus(OrderStatusEnum.SHIPPED.getCode());
        return toAjax(crmOrderService.updateCrmOrder(crmOrder));
    }





    /**
     * 查询客户订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrmOrder crmOrder)
    {
        startPage();
        List<CrmOrder> list = crmOrderService.selectCrmOrderList(crmOrder);
        return getDataTable(list);
    }

    /**
     * 导出客户订单列表
     */
    @PreAuthorize("@ss.hasPermi('crm:order:export')")
    @Log(title = "客户订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmOrder crmOrder)
    {
        List<CrmOrder> list = crmOrderService.selectCrmOrderList(crmOrder);
        ExcelUtil<CrmOrder> util = new ExcelUtil<CrmOrder>(CrmOrder.class);
        util.exportExcel(response, list, "客户订单数据");
    }

    /**
     * 获取客户订单详细信息
     */

    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(crmOrderService.selectCrmOrderByOrderId(orderId));
    }

    /**
     * 新增客户订单
     */

    @Log(title = "客户订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmOrder crmOrder)
    {
        if (StringUtils.isNull(crmOrder.getOrderPrice())){
            return error("单价不能为空");
        }
        if (StringUtils.isNull(crmOrder.getOrderImg())){
            return error("总价不能为空");
        }
        if (StringUtils.isNull(crmOrder.getOrderNumber())){
            return error("数量不能为空");
        }
        if (StringUtils.isNull(crmOrder.getOrderMork())){
            return error("牌号不能为空");
        }

        return toAjax(crmOrderService.insertCrmOrder(crmOrder));
    }

    /**
     * 修改客户订单
     */
    @PreAuthorize("@ss.hasPermi('crm:order:edit')")
    @Log(title = "客户订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmOrder crmOrder)
    {
        return toAjax(crmOrderService.updateCrmOrder(crmOrder));
    }

    /**
     * 删除客户订单
     */
    @PreAuthorize("@ss.hasPermi('crm:order:remove')")
    @Log(title = "客户订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(crmOrderService.deleteCrmOrderByOrderIds(orderIds));
    }
}
